const moment = require('moment');
import {
  timeStampFormat
} from './DateFormat.js'

const ShortTimeType = {
  today: 0, //今日
  lastDay: 1, //昨日
  currentWeek: 2, //本周
  lastWeek: 3, //上周
  currentMonth: 4, //本月
  lastMonth: 5, //上月
  currentYear: 6, //本年
  lastYear: 7, //上年
  customer: 99, //自定义选择时间；会重新赋值
}


/**
 * 快捷时间
 */
class ShortTime {
  constructor(type) {
    this.type = type;
  }
  set type(type) {
    this._type = type;
    this.name = this.setupName()
  }
  get type() {
    return this._type;
  }
  setupName() {
    switch (this._type) {
      case ShortTimeType.today:
        return '今日';
      case ShortTimeType.lastDay:
        return '昨日';
      case ShortTimeType.currentWeek:
        return '本周';
      case ShortTimeType.lastWeek:
        return '上周';
      case ShortTimeType.currentMonth:
        return '本月';
      case ShortTimeType.lastMonth:
        return '上月';
      case ShortTimeType.currentYear:
        return '今年';
      case ShortTimeType.lastYear:
        return '去年';
      case ShortTimeType.customer:
        if (this._start && this._end) {
          return timeStampFormat(this._start, 'YYYY-MM-DD') + '-' + timeStampFormat(this._end, 'YYYY-MM-DD')
        } else {
          return '自定义'
        }

    }
    return '';
  }


  set start(start) {
    this._start = start;
    if (this._type == ShortTimeType.customer) {
      this.showName = this.setupName()
      this.name = "自定义"
    } else {
      this.name = this.setupName()
    }
  }

  get start() {
    switch (this._type) {
      case ShortTimeType.today:
        return this.getOneDayStartEnd().start;
      case ShortTimeType.lastDay:
        return this.getYesterday().start;
      case ShortTimeType.currentWeek:
        return this.getThisWeek().start;
      case ShortTimeType.lastWeek:
        return this.getLastWeek().start;
      case ShortTimeType.currentMonth:
        return this.getThisMonth().start;
      case ShortTimeType.lastMonth:
        return this.getLastMonth().start;
      case ShortTimeType.currentYear:
        return this.getThisYear().start;
      case ShortTimeType.lastYear:
        return this.getLastYear().start;


      case ShortTimeType.customer:
        return this._start
    }
  }

  set end(end) {
    this._end = end;

    if (this._type == ShortTimeType.customer) {
      this.showName = this.setupName()
      this.name = "自定义"
    } else {
      this.name = this.setupName()
    }

  }
  get end() {
    switch (this._type) {
      case ShortTimeType.today:
        return this.getOneDayStartEnd().end;
      case ShortTimeType.lastDay:
        return this.getYesterday().end;
      case ShortTimeType.currentWeek:
        return this.getThisWeek().end;
      case ShortTimeType.lastWeek:
        return this.getLastWeek().end;
      case ShortTimeType.currentMonth:
        return this.getThisMonth().end;
      case ShortTimeType.lastMonth:
        return this.getLastMonth().end;
      case ShortTimeType.currentYear:
        return this.getThisYear().end;
      case ShortTimeType.lastYear:
        return this.getLastYear().end;
      case ShortTimeType.customer:
        return this._end;
    }

  }

  // 获取某天开始结束
  getOneDayStartEnd = function(date = new Date()) {
    var start = moment(new Date()).startOf('day').unix() * 1000
    var end = moment(new Date()).endOf('day').unix() * 1000
    return {
      start: start,
      end: end
    }
  }

  getYesterday = function() {
    var start = moment(new Date()).subtract(1, 'day').startOf('day').unix() * 1000
    var end = moment(new Date()).subtract(1, 'day').endOf('day').unix() * 1000
    return {
      start: start,
      end: end
    };
  }

  getThisWeek = function() {
    var start = moment(new Date()).startOf('week').subtract(-1, 'day').unix() * 1000
    var end = moment(new Date()).endOf('week').subtract(-1, 'day').unix() * 1000
    return {
      start: start,
      end: end
    };
  }

  getLastWeek = function() {
    var start = moment(new Date()).startOf('week').subtract(-1, 'day').subtract(1, 'week').unix() * 1000
    var end = moment(new Date()).endOf('week').subtract(-1, 'day').subtract(1, 'week').unix() * 1000
    return {
      start: start,
      end: end
    };
  }

  getNextWeek = function() {
    var start = moment(new Date()).startOf('week').subtract(-1, 'day').subtract(-1, 'week').unix() * 1000
    var end = moment(new Date()).endOf('week').subtract(-1, 'day').subtract(-1, 'week').unix() * 1000
    return {
      start: start,
      end: end
    };
  }

  getThisMonth = function() {
    var start = moment(new Date()).startOf('month').unix() * 1000
    var end = moment(new Date()).endOf('month').unix() * 1000
    return {
      start: start,
      end: end
    }
  }

  getLastMonth = function() {
    var start = moment(new Date()).startOf('month').subtract(1, 'month').unix() * 1000
    var end = moment(new Date()).endOf('month').subtract(1, 'month').unix() * 1000
    return {
      start: start,
      end: end
    }
  }
  getThisYear= function () {
    var start = moment(new Date()).startOf('year').unix() * 1000
    var end = moment(new Date()).endOf('year').unix() * 1000
    return {
      start: start,
      end: end
    }
  }

  getLastYear = function () {
    var start = moment(new Date()).startOf('year').subtract(1, 'year').unix() * 1000
    var end = moment(new Date()).endOf('year').subtract(1, 'year').unix() * 1000
    return {
      start: start,
      end: end
    }
  }
}

module.exports = {
  ShortTimeType: ShortTimeType,
  ShortTime: ShortTime
}